package org.code.mallspringboot.controller;

import org.code.mallspringboot.dto.Oms;
import org.code.mallspringboot.pojo.OmsOrder;
import org.code.mallspringboot.service.OmsOrderItemService;
import org.code.mallspringboot.service.OmsOrderOperateHistoryService;
import org.code.mallspringboot.service.OmsOrderService;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OmsOrderController {
    @Autowired
    private OmsOrderService orderService;
    @Autowired
    private OmsOrderItemService orderItemService;
    @Autowired
    private OmsOrderOperateHistoryService orderOperateHistoryService;
    @GetMapping("/list")
    public CommonResult list(int pageNum, int pageSize, String orderSn, String receiverKeyword, String status, String orderType, String sourceType,String createTime) {
        int page=(pageNum-1)*pageSize;
        List<OmsOrder> list= orderService.selectAll(page,pageSize,orderSn,receiverKeyword,status,orderType,sourceType,createTime);
        int total= orderService.count(orderSn,receiverKeyword,status,orderType,sourceType,createTime);
        Map map=new HashMap();
        map.put("total",total);
        map.put("list",list);
        return CommonResult.success(map);
    }
    @GetMapping("{id}")
    public CommonResult detail(@PathVariable("id") String id) {
        OmsOrder omsOrder=orderService.selectAllByIdInt(id);
        omsOrder.setOrderItemList(orderItemService.listByOrderId(Long.valueOf(id)));
        omsOrder.setHistoryList(orderOperateHistoryService.selectByOrderId(Long.valueOf(id)));
        return CommonResult.success(omsOrder);
    }
    @PostMapping("/update/delivery")
    public CommonResult updateDelivery(@RequestBody Oms[] oms) {
        for (Oms om : oms) {
            int i=orderService.updateDeliverySnInt(om.getDeliverySn(),om.getDeliveryCompany(), String.valueOf(om.getOrderId()));
            if(i<0){
                return CommonResult.failed();
            }
        }
        return CommonResult.success("");
    }
    @PostMapping("/update/receiverInfo")
    public CommonResult updateReceiverInfo(@RequestBody Oms order) {
        int i=orderService.updateReceiverNameInt(order);
        if(i<0){
            return CommonResult.failed();
        }
        return CommonResult.success("");
    }
    @PostMapping("/update/note")
    public CommonResult updateNote(String note,String id,String status) {
        int i= orderService.updateNoteInt(note, id);
        if(i<0){
            return CommonResult.failed();
        }
        return CommonResult.success("");
    }
    @PostMapping("/delete")
    public CommonResult delete(String[] ids) {
        for (String id : ids) {
            if (orderService.updateDeleteStatusInt(id)<0){
                return CommonResult.failed();
            }
        }
        return CommonResult.success("");
    }
}